﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace De8_QLyMuonsach
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Muonsach> lstMuonsach = new List<Muonsach>();
        List<Muonsach> lstkiemtra = new List<Muonsach>();
        public void Nhap()
        {
            Muonsach a = new Muonsach();
            a.MADOCGIA = txtMadg.Text;
            a.MASACH = txtMasach.Text;
            a.NGAYMUON = Convert.ToDateTime(dtpNgaymuon.Text);
            a.THOIHAN = int.Parse(txtThoihan.Text);           
            if(txtMadg.Text == "" || txtMasach.Text == "" || txtThoihan.Text == "")
                MessageBox.Show("Còn giá trị rỗng !!! Vui lòng nhập lại!!!!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            { 
                lstMuonsach.Add(a);
                ResetText();          
            }           
        }
        public void Nhapkt()
        {
            Muonsach a = new Muonsach();
            a.MADOCGIA = txtMadocgiakt.Text;
            lstkiemtra.Add(a);
            ResetText();
        }

        public void HienThi(Muonsach a)
        {
            ListViewItem item = new ListViewItem();
            item.Text = a.MADOCGIA;
            item.SubItems.Add(a.MASACH);
            item.SubItems.Add(a.NGAYMUON.ToShortDateString());
            item.SubItems.Add(a.THOIHAN.ToString());
            listView1.Items.Add(item);
        }
        public void ResetLV()
        {
            try
            {
                int i = listView1.Items.Count;
                for (int j = i; j >= 0; j--)
                    listView1.Items.RemoveAt(0);
            }
            catch
            { };
        }
        public void Resettext()
        {
            txtMadg.Text = " ";
            txtMasach.Text = " ";
            dtpNgaymuon.Text = "08/01/2014";
            txtThoihan.Text = "0";
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            ResetLV();
            Nhap();
            foreach (Muonsach a in lstMuonsach)
            HienThi(a);
            }
        public void Tinh()
        {
            Nhapkt();
            int tong = 0;
            foreach (Muonsach a in lstMuonsach)
            {
                if (txtMadocgiakt.Text == a.MADOCGIA)
                {
                    tong = tong + 1;
                }
            }
            txtKq.Text = tong.ToString();
        }
        private void btnTinh_Click(object sender, EventArgs e)
        {
            Tinh();
        }
        public void Kiemtra()
        {
            foreach (Muonsach a in lstMuonsach)
            {
                DateTime n = Convert.ToDateTime(dtpNgaykiemtra.Text);
                DateTime m = a.NGAYMUON.AddDays(a.THOIHAN);
                if (n == m)
                    HienThi(a);
            }
        }

        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            ResetLV();
            Kiemtra();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            ResetLV();
            foreach (Muonsach a in lstMuonsach)
                HienThi(a);
        }
    }    
}
